/**
 * Created by chenyong on 2015/3/22.
 */

jQuery.support.cors = true;
(function ($) {
    var settings = {
        'debug': false,
        'timeout': 120
    };

    var currentUser = {};

    var loginpop = null;


    var methods = {
        //测试模式
        debug: function () {
            return settings.debug;
        },
        set: function (setting, value) {
            return set(setting, value)
        },
        pano: function () {
            return document.getElementById("panoSWFObject");
        },
        //登录（QQ,微信，微博）
        login: function(type, success_callback, failed_callback){
            switch (type) {
                case 'qq':
                    login_sequence_qq(success_callback, failed_callback);
                    break;
                case 'wechat':
                    login_sequence_wechat(success_callback, failed_callback);
                    break;
                case 'weibo':
                    login_sequence_weibo(success_callback, failed_callback);
                    break;
                default:
                    return false;
            }
        },
        //检查是否已经登陆
        authCheck: function(success_callback,failed_callback){
            if($.cookie('token') == undefined){
                if (failed_callback != undefined) {
                    if (typeof failed_callback == 'function') {
                        failed_callback.call(this);
                    }
                }
            }
            else if (currentUser.provider == undefined) {
                var _authToken = "Bearer " + $.cookie('token').replace(/\"/g, '');
                $.ajax({
                    url: '/auth/me',
                    type: 'GET',
                    dataType: 'json',
                    cache: false,
                    async:false,
                    beforeSend: function(req) {
                        req.setRequestHeader("Authorization", _authToken);
                    },
                    complete:function(data,status){
                        //alert('complete');
                    },
                    success: function (data,status) {
                        currentUser = data;
                        console.log(data);
                        success_callback.call(this);
                    },
                    error: function (err) {
                        if (failed_callback != undefined) {
                            if (typeof failed_callback == 'function') {
                                failed_callback.call(this);
                            }
                        }
                    }
                });
            }
            else
            {
                if (success_callback != undefined) {
                    if (typeof success_callback == 'function') {
                        success_callback.call(this);
                    }
                }
            }
        },
        //登录
        auth: function (type, success_callback, failed_callback) {
            switch (type) {
                case 'qq':
                    authentication_with_qq(success_callback, failed_callback);
                    break;
                case 'wechat':
                    authentication_with_wechat(success_callback, failed_callback);
                    break;
                case 'weibo':
                    authentication_with_weibo(success_callback, failed_callback);
                    break;
                default:
                    return false;
            }
        },
        me: function () {
            var _me = {id:'', name:'', network:''};
            var _isme = false;
            //if(currentUser.provider != undefined){
                _me.id = currentUser._id;
                _me.name = currentUser.name;
                //_me.network = currentUser.provider;
                _me.network = 'wechat';
                _isme = true;
            //}
            $.bigpixel.pano().set('authenticated', _isme);
            if (_isme)
            {
                return _me;
            }
            else
            {
                return _isme;
            }
        },
        login_success: function () {
            //if(loginpop != null)
            //{
                //loginpop.close();
            //}
            console.log('login success');
        }
    };

    $.bigpixel = methods;

    var onloginsuccess;

    //使用QQ登录
    function authentication_with_qq(success_callback, failed_callback){
        $.bigpixel.login_success = success_callback;
        if(currentUser.provider == undefined){
            loginpop = window.open("/auth/qq/", "_blank", "location=0,status=0,scrollbars=0,menubar=0,resizable=0");
        }
    }
    function qqlogin_success()
    {
        alert('qqlogin_success');
    }

    //QQ登录系列动作
    function login_sequence_qq(success_callback, failed_callback) {
        $.bigpixel.auth('qq', function () {
                if ($.bigpixel.debug()) {
                    console.log("Authentication with Twitter :: Success");
                }
                $.bigpixel.saveNetworkUser('qq');
                $.bigpixel.logFriends('qq', function () {
                        if (success_callback != undefined) {
                            if (typeof success_callback == 'function') {
                                success_callback.call(this);
                            }
                        }
                    },
                    function () {
                        if (failed_callback != undefined) {
                            if (typeof failed_callback == 'function') {
                                failed_callback.call(this);
                            }
                        }
                    });
            },
            function () {
                if ($.bigpixel.debug()) {
                    console.log("Authentication with Twitter :: Failed");
                }
                if (failed_callback != undefined) {
                    if (typeof failed_callback == 'function') {
                        failed_callback.call(this);
                    }
                }
            });
    }




    //使用微信登录
    function authentication_with_wechat(success_callback, failed_callback){
        $.bigpixel.login_success = success_callback;
        if(currentUser.provider == undefined){
            loginpop = window.open("/auth/wechat/", "_blank", "location=0,status=0,scrollbars=0,menubar=0,resizable=0");
        }
    }
    function wechatlogin_success()
    {
        alert('wechatlogin_success');
    }

    //微信登录系列动作
    function login_sequence_wechat(success_callback, failed_callback) {
        $.bigpixel.auth('wechat', function () {
                if ($.bigpixel.debug()) {
                    console.log("Authentication with Twitter :: Success");
                }
                $.bigpixel.saveNetworkUser('wechat');
                $.bigpixel.logFriends('wechat', function () {
                        if (success_callback != undefined) {
                            if (typeof success_callback == 'function') {
                                success_callback.call(this);
                            }
                        }
                    },
                    function () {
                        if (failed_callback != undefined) {
                            if (typeof failed_callback == 'function') {
                                failed_callback.call(this);
                            }
                        }
                    });
            },
            function () {
                if ($.bigpixel.debug()) {
                    console.log("Authentication with Twitter :: Failed");
                }
                if (failed_callback != undefined) {
                    if (typeof failed_callback == 'function') {
                        failed_callback.call(this);
                    }
                }
            });
    }


})(jQuery);